<?php
/**
 * 应用公共上传控制器
 *
 * @author      jinzhanyou
 * @version     1.0
 */

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    /**
     * @apiVersion 1.0.0
     * @apiGroup Uploads
     * @apiDescription 上传图片 接口 路由别名：upload.image
     * @apiName 上传图片
     * @api {post} /common/upload/image 上传图片
     * @apiParam {image-file} file 文件 必填
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "上传成功",
     *              "url": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              "data": [
     *                  "src": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              ]
     *         }
     *     }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "上传失败"
     *         }
     *     }
     * */
    public function image(Request $request)
    {
        $maxSize = 10;
        $allowed_extensions = ['png', 'jpg', 'gif'];
        $data = ['code' => self::RESPONSE_CODE_ERROR, 'msg' => '上传失败'];
        $file = $request->file('file');
        if (!$file) {
            $data = ['code' => self::RESPONSE_CODE_ERROR, 'msg' => '上传失败'];
            return response()->json($data);
        }
        if ($file->isValid()) {
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                $data['msg'] = '请上传' . implode(',', $allowed_extensions) . '格式的图片';
                return response()->json($data);
            }
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                $data['msg'] = '图片大小限制' . $maxSize . 'M';
                return response()->json($data);
            }
        } else {
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }
        $dir = date('Ymd');
        $newFile = $dir . '/' . time() . uniqid() . '.' . $file->getClientOriginalExtension();
        $realPath = $file->getRealPath();
        $bool = Storage::disk('uploads')->put($newFile, File::get($realPath));
        if ($bool) {
            $data = [
                'code' => self::RESPONSE_CODE_SUCCESS,
                'msg' => '上传成功',
                'url' => config('app_url') . '/uploads/' . $newFile,
                'data' => [
                    'src' => config('app_url') . '/uploads/' . $newFile
                ]
            ];
        } else {
            $data['data'] = $file->getErrorMessage();
        }
        return response()->json($data);
    }

    /**
     * @apiVersion 1.0.0
     * @apiGroup Uploads
     * @apiDescription 编辑器 上传图片 接口 路由别名：editor.upload.image
     * @apiName 编辑器 上传图片
     * @api {post} /common/upload/editor/image 编辑器 上传图片
     * @apiParam {image-file} file 文件 必填
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "上传成功",
     *              "url": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              "data": [
     *                  "src": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              ]
     *         }
     *     }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "上传失败"
     *         }
     *     }
     * */
    public function editorUploadImage(Request $request)
    {
        $maxSize = 10;
        $allowed_extensions = ['png', 'jpg', 'gif'];
        $data = ['code' => self::RESPONSE_CODE_ERROR, 'msg' => '上传失败'];
        $file = $request->file('file');
        if (!$file) {
            $data = ['code' => self::RESPONSE_CODE_ERROR, 'msg' => '上传失败'];
            return response()->json($data);
        }
        if ($file->isValid()) {
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                $data['msg'] = '请上传' . implode(',', $allowed_extensions) . '格式的图片';
                return response()->json($data);
            }
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                $data['msg'] = '图片大小限制' . $maxSize . 'M';
                return response()->json($data);
            }
        } else {
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }
        $dir = date('Ymd');
        $newFile = $dir . '/' . time() . uniqid() . '.' . $file->getClientOriginalExtension();
        $realPath = $file->getRealPath();
        $bool = Storage::disk('editor_uploads')->put($newFile, File::get($realPath));
        if ($bool) {
            $data = [
                'code' => self::RESPONSE_CODE_SUCCESS,
                'msg' => '上传成功',
                'data' => [
                    'src' => config('app_url') . '/uploads/editor/' . $newFile
                ]
            ];
        } else {
            $data['data'] = $file->getErrorMessage();
        }
        return response()->json($data);
    }

    /**
     * @apiVersion 1.0.0
     * @apiGroup Uploads
     * @apiDescription 视频 接口 路由别名：upload.video
     * @apiName 视频
     * @api {post} /common/upload/video 视频
     * @apiParam {image-file} file 文件 必填
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "上传成功",
     *              "url": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              "data": [
     *                  "src": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              ]
     *         }
     *     }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "上传失败"
     *         }
     *     }
     * */
    public function video(Request $request)
    {
        $allowed_extensions = ['mp4', 'wmv', 'wav'];
        $data = ['code' => self::RESPONSE_CODE_ERROR, 'msg' => '上传失败'];
        $file = $request->file('file');
        if (!$file) {
            $data = ['code' => self::RESPONSE_CODE_ERROR, 'msg' => '上传失败'];
            return response()->json($data);
        }
        if ($file->isValid()) {
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                $data['msg'] = '请上传' . implode(',', $allowed_extensions) . '格式的视频';
                return response()->json($data);
            }
        } else {
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }
        $dir = date('Ymd');
        $newFile = $dir . '/' . time() . uniqid() . '.' . $file->getClientOriginalExtension();
        $realPath = $file->getRealPath();
        $bool = Storage::disk('video')->put($newFile, File::get($realPath));
        if ($bool) {
            $data = [
                'code' => self::RESPONSE_CODE_SUCCESS,
                'msg' => '上传成功',
                'url' => config('app_url') . '/uploads/video/' . $newFile,
                'data' => [
                    'src' => config('app_url') . '/uploads/video/' . $newFile
                ]
            ];
        } else {
            $data['data'] = $file->getErrorMessage();
        }
        return response()->json($data);
    }

    /**
     * @apiVersion 1.0.0
     * @apiGroup Uploads
     * @apiDescription upload 文件 接口 路由别名：upload.file
     * @apiName upload 文件
     * @api {post} /common/upload/file upload 文件
     * @apiParam {image-file} file 文件 必填
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "上传成功",
     *              "url": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              "data": [
     *                  "src": "/uploads/20190709/15626549845d243908f257e.jpg"
     *              ]
     *         }
     *     }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 200
     *     {
     *         "data": {
     *             "code": 1,
     *             "msg": "请重新上传"
     *         }
     *     }
     * */
    public function file(Request $request)
    {
        if ($request->hasFile('file')) {
            $file = $request->file('file'); // 获取上传的文件
            $dir = date('Ymd');
            $newFile = $dir . "/" . time() . uniqid() . "." . $file->getClientOriginalExtension();
            $realPath = $file->getRealPath();
            $bool = Storage::disk('file')->put($newFile, File::get($realPath));
            if ($bool) {
                return response([
                    'code' => self::RESPONSE_CODE_SUCCESS,
                    'message' => '上传成功',
                    'url' => config('app_url') . '/uploads/file/' . $newFile,
                    'data' => [
                        'src' => config('app_url') . '/uploads/file/' . $newFile
                    ]
                ]);
            } else {
                return response([
                    'code' => self::RESPONSE_CODE_ERROR,
                    'message' => '请重新上传'
                ]);
            }
        } else {
            return response([
                'code' => self::RESPONSE_CODE_ERROR,
                'message' => '请重新上传'
            ]);
        }
    }
}